package com.java.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.java.entity.BsAppraise;
import com.java.entity.BsStudent;
import com.java.entity.BsScore;
import com.java.service.BsStudentService;
import com.java.service.BsAppraiseService;
import com.java.service.BsCourseService;
import com.java.service.BsPaperService;
import com.java.service.BsStudentService;
import com.java.util.MyCons;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;
import com.java.util.servlet.ServletUtil;

@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired private BsStudentService studentService;
	@Autowired private BsCourseService courseService;
	@Autowired private BsAppraiseService appraiseService;
	@Autowired private BsPaperService paperService;
	
	@PostMapping("/info")
	public BaseRespone info() {
		BsStudent user = (BsStudent) ServletUtil.getSession().getAttribute(MyCons.USER_ROLE);
		BsStudent student = studentService.getStudent(user.getStudentId());
		return BaseRespone.success(student);
	}
	
	@PostMapping("/saveInfo")
	public BaseRespone saveInfo(@RequestBody BsStudent student) {
		return studentService.update(student);
	}
	
	/**
	 * 课程与目标操作
	 */
	
	@PostMapping("/getCourses")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm",timezone = "GMT+8")
	public TableRespone getCourses(
			@RequestParam(required = false) String title, 
			@RequestParam(required = false) String typeId, 
			@RequestParam(required = false, defaultValue = "0") int page, 
			@RequestParam(required = false, defaultValue = "0") int size) {
		return courseService.getCourses(title, typeId, page, size);
	}
	
	@PostMapping("/getTargets")
	public TableRespone getTargets(String courseId) {
		return courseService.getTargets(courseId);
	}
	
	
	/**
	 * 成绩操作
	 */
	
	@PostMapping("/getScores")
	public TableRespone getScores(
			@RequestParam(required = false) String title, 
			@RequestParam(required = false) String typeId, 
			@RequestParam(required = false, defaultValue = "0") int page, 
			@RequestParam(required = false, defaultValue = "0") int size) {
		BsStudent user = (BsStudent) ServletUtil.getSession().getAttribute(MyCons.USER_ROLE);
		return studentService.getScores(user.getStudentId(), title, typeId, page, size);
	}
	
	@PostMapping("/getScoreRecords")
	public TableRespone getScoreRecords(String scoreId) {
		return studentService.getRecords(scoreId);
	}
	
	@PostMapping("/saveScore")
	public BaseRespone saveScore(@RequestBody BsScore score) {
		return studentService.saveScore(score);
	}
	
	@PostMapping("/delScore")
	public BaseRespone delScore(String scoreId) {
		return studentService.delScore(scoreId);
	}
	
	/**
	 * 考核记录与目标分值操作
	 */
	
	@PostMapping("/getAppraises")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm",timezone = "GMT+8")
	public TableRespone getAppraises(
			@RequestParam(required = false) String courseId, 
			@RequestParam(required = false) String remark, 
			@RequestParam(required = false, defaultValue = "0") int page, 
			@RequestParam(required = false, defaultValue = "0") int size) {
		BsStudent user = (BsStudent) ServletUtil.getSession().getAttribute(MyCons.USER_ROLE);
		return appraiseService.getAppraises(user.getStudentId(), courseId, remark, page, size);
	}
	
	@PostMapping("/getRecords")
	public TableRespone getRecords(String appraiseId) {
		return appraiseService.getRecords(appraiseId);
	}
	
	@PostMapping("/getDegrees")
	public TableRespone getDegrees(
			@RequestParam(required = false) String courseId, 
			@RequestParam(required = false) String targetId, 
			@RequestParam(required = false) String remark, 
			@RequestParam(required = false) String date, 
			@RequestParam(required = false, defaultValue = "0") int page, 
			@RequestParam(required = false, defaultValue = "0") int size) {
		BsStudent user = (BsStudent) ServletUtil.getSession().getAttribute(MyCons.USER_ROLE);
		return appraiseService.getDegrees(user.getStudentId(), courseId, targetId, remark, date, page, size);
	}
}
